<?php
namespace app\api\controller;

use think\Config;
use think\Controller;
use think\Cookie;
use think\Request;
use think\Db;
use DfaFilter\SensitiveHelper;
use think\Image;
use OSS\OssClient;
use OSS\Core\OssException;

class Index extends Base {

    public function __construct(){
        $url = isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : '';
    }

    /**
     * Hello World
     */
    public function index(Request $request) {
        echo 'Hello World';
    }

    // +----------------------------------------------------------------------
    // | index接口初始板块（2019/08/27）
    // +----------------------------------------------------------------------

    //接口请求 TODO 线上
    public function apppost(){
         header("Content-type: text/html; charset=UTF-8");
//        header("Content-type: application/json; charset=UTF-8");
        if(request()->isPost()){
           $this->init();
        }else{
            $this->alertMsg(2, '请求错误');
        }
    }


    //接口请求 TODO 本地测试时接口请求
    public function appget(){
//        header("Content-type: application/json; charset=UTF-8");
         header("Content-type: text/html; charset=UTF-8");
        if(request()->isGet()){
            $this->init();
        }else{
            $this->alertMsg(2, '请求错误');
        }
    }


    //接口请求 TODO 本地测试时接口请求
    public function appget2(){
        header("Content-type: text/html; charset=UTF-8");
        if(request()->isGet()){
            $this->init2();
        }else{
            $this->alertMsg(2,'请求错误');
        }
    }


    //公共方法
    private function init(){
        if(input('')){
            $param =str_replace('/','\\',input('action'));
            $urlArr = explode('\\', $param);
            if(!empty($urlArr)){
                $className = 'app\\'.request()->module().'\\controller\\'.ucfirst($urlArr[0]);
                if(class_exists($className)){
                    try{
                        $mothod = new $className();
                        if(method_exists($mothod,$urlArr[1])){

                        }else{
                            $this->alertMsg(6, '参数错误1');
                        }
                    } catch (\Exception $exception) {
                        $this->alertMsg(5, '参数错误2');
                    }

                    //执行接口
                    $mothod->{$urlArr[1]}(input());

                }else{
                    $this->alertMsg(4, '参数错误3');
                }
            }
        }else{
            $this->alertMsg(3, '系统错误4');//访问接口参数错误
        }
    }


    //公共方法（2；3方法名错误）
    private function init2(){
        $action = 'Login/login';
//        $action = 'Login/getHousingInfo';
//        $action = 'Login/perfectUserInfo';
//        $action = 'Login/openid';
       // if(input('')){
        if($action){
           // $param =str_replace('/','\\',input('action'));
            $param =str_replace('/','\\',$action);
            $urlArr = explode('\\', $param);
            if(!empty($urlArr)){
                $className = 'app\\'.request()->module().'\\controller\\'.ucfirst($urlArr[0]);
                if(class_exists($className)){
                    try{
                        $mothod = new $className();
                        if(method_exists($mothod,$urlArr[1])){

                        }else{
                            $this->alertMsg(51, '参数错误1');
                        }
                    } catch (\Exception $exception) {
                        $this->alertMsg(44, '参数错误2');
                    }

                    //执行接口
                    $mothod->{$urlArr[1]}(input());

                }else{
                    $this->alertMsg(50, '参数错误3');
                }
            }
        }else{
            $this->alertMsg(44,'系统错误');
        }
    }
}